############################################################
## Fraga, Juenke, and Shah 2019: One Run Leads to Another?
## Replication Code: January 2, 2018
## Figure A4: Candidate Emergence, African-Americans and Latinos conditional on other group %
############################################################

# Required Libraries
require(ggplot2)

# Load Data #
sl_data <- read.csv("FragaJuenkeShah_JOP_Data.csv", stringsAsFactors=FALSE)

# Remove all districts where 1+ candidates have no race data
sl_data <- subset(sl_data, unknown_cand == 0)

# Change candidate variable to a binary, 0 = no candidate, 1 = one or more candidates
sl_data$white_cand[sl_data$white_cand > 1] <- 1
sl_data$black_cand[sl_data$black_cand > 1] <- 1
sl_data$latino_cand[sl_data$latino_cand > 1] <- 1
sl_data$asian_cand[sl_data$asian_cand > 1] <- 1

## Create four bins of percent Black
sl_data$BlackBin <- "<10% Black"
sl_data$BlackBin[sl_data$black_pct > 0.10 & sl_data$black_pct < 0.25] <- "10-25% Black"
sl_data$BlackBin[sl_data$black_pct > 0.25 & sl_data$black_pct < 0.50] <- "25-50% Black"
sl_data$BlackBin[sl_data$black_pct >= 0.5]<- "≥50% Black"
sl_data$BlackBin <- factor(sl_data$BlackBin, levels=c("<10% Black", "10-25% Black", "25-50% Black", "≥50% Black"), labels=c("<10% Black", "10-25% Black", "25-50% Black", "≥50% Black"))

## Create four bins of percent Latino
sl_data$LatinoBin <- "<10% Latino"
sl_data$LatinoBin[sl_data$latino_pct > 0.10 & sl_data$latino_pct < 0.25] <- "10-25% Latino"
sl_data$LatinoBin[sl_data$latino_pct > 0.25 & sl_data$latino_pct < 0.50] <- "25-50% Latino"
sl_data$LatinoBin[sl_data$latino_pct >= 0.5]<- "≥50% Latino"
sl_data$LatinoBin <- factor(sl_data$LatinoBin, levels=c("<10% Latino", "10-25% Latino", "25-50% Latino", "≥50% Latino"), labels=c("<10% Latino", "10-25% Latino", "25-50% Latino", "≥50% Latino"))

## FigA3a, Left Panel of Figure A3: African-Americans
figA3a <- ggplot(data=sl_data, aes(x=black_pct, y=black_cand))
figA3a <- figA3a + geom_segment(aes(y=0.5,yend=0.5,x=-Inf,xend=Inf), size=0.5, linetype=2, lineend="round", color="gray70")
figA3a <- figA3a + stat_smooth(aes(color=LatinoBin), method = "loess", size=1.5, se=FALSE)
figA3a <- figA3a + geom_point(alpha=0.2)
figA3a <- figA3a + ggtitle("African-Americans, Conditional on % Percent Latino")
figA3a <- figA3a + scale_x_continuous("Percent Black in State Legislative District")
figA3a <- figA3a + scale_y_continuous("Probability of Black Candidate", limits=c(-0.05,1.05))
figA3a <- figA3a + coord_cartesian(ylim=c(0,1))
figA3a <- figA3a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.position=c(0.8,0.3),
	legend.direction="vertical",
	legend.title=element_blank())
figA3a

## FigA3b, Right Panel of Figure A3: Latinos
figA3b <- ggplot(data=sl_data, aes(x=latino_pct, y=latino_cand))
figA3b <- figA3b + geom_segment(aes(y=0.5,yend=0.5,x=-Inf,xend=Inf), size=0.5, linetype=2, lineend="round", color="gray70")
figA3b <- figA3b + stat_smooth(aes(color=BlackBin), method = "loess", size=1.5, se=FALSE)
figA3b <- figA3b + geom_point(alpha=0.2)
figA3b <- figA3b + ggtitle("Latinos, Conditional on % Percent Black")
figA3b <- figA3b + scale_x_continuous("Percent Latino in State Legislative District")
figA3b <- figA3b + scale_y_continuous("Probability of Latino Candidate", limits=c(-0.05,1.05))
figA3b <- figA3b + coord_cartesian(ylim=c(0,1))
figA3b <- figA3b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.position=c(0.8,0.3),
	legend.direction="vertical",
	legend.title=element_blank())
figA3b